import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 自定义导航栏组件
/// 处理手机头部安全区域并适配不同设备
class CustomNavbar extends StatelessWidget {
  /// 导航栏标题 (当使用默认布局时)
  final String title;
  
  /// 是否显示返回按钮 (当使用默认布局时)
  final bool showBackButton;
  
  /// 返回按钮点击回调
  final VoidCallback? onBackPress;
  
  /// 右侧操作按钮 (当使用默认布局时)
  final List<Widget>? actions;
  
  /// 自定义导航栏内容
  /// 如果提供了自定义内容，将优先显示自定义内容，而不是默认的标题+按钮布局
  final Widget? customContent;
  
  /// 导航栏背景色
  final Color? backgroundColor;
  
  /// 导航栏标题颜色 (当使用默认布局时)
  final Color? titleColor;
  
  /// 导航栏阴影
  final bool showShadow;
  
  /// 是否使用沉浸式状态栏
  final bool useImmersiveStatusBar;

  const CustomNavbar({
    Key? key,
    required this.title,
    this.showBackButton = false,
    this.onBackPress,
    this.actions,
    this.customContent,
    this.backgroundColor = Colors.white,
    this.titleColor = Colors.black,
    this.showShadow = true,
    this.useImmersiveStatusBar = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 配置状态栏样式
    if (useImmersiveStatusBar) {
      SystemChrome.setSystemUIOverlayStyle(
        SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          statusBarIconBrightness: 
              backgroundColor != null && backgroundColor!.computeLuminance() > 0.5
                  ? Brightness.dark
                  : Brightness.light,
          systemNavigationBarColor: backgroundColor,
        ),
      );
    }

    return Container(
      color: backgroundColor,
      child: SafeArea(
        top: true,
        bottom: false,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              height: kToolbarHeight,
              padding: EdgeInsets.symmetric(
                horizontal: 16.0,
              ),
              decoration: BoxDecoration(
                color: backgroundColor,
                boxShadow: showShadow
                    ? [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.1),
                          blurRadius: 2.0,
                          spreadRadius: 1.0,
                        ),
                      ]
                    : null,
              ),
              child: customContent != null
                  ? customContent
                  : Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        // 左侧返回按钮
                        showBackButton
                            ? IconButton(
                                icon: Icon(
                                  Icons.arrow_back_ios,
                                  color: titleColor,
                                  size: 20.0,
                                ),
                                padding: EdgeInsets.zero,
                                onPressed: onBackPress ?? () => Navigator.pop(context),
                                constraints: BoxConstraints(
                                  minWidth: 40.0,
                                  minHeight: 40.0,
                                ),
                              )
                            : SizedBox(width: 40.0),
                        
                        // 中间标题
                        Expanded(
                          child: Center(
                            child: Text(
                              title,
                              style: TextStyle(
                                fontSize: 18.0,
                                fontWeight: FontWeight.w600,
                                color: titleColor,
                              ),
                              overflow: TextOverflow.ellipsis,
                              maxLines: 1,
                            ),
                          ),
                        ),
                        
                        // 右侧操作按钮
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: actions ?? [SizedBox(width: 40.0)],
                        ),
                      ],
                    ),
            ),
          ],
        ),
      ),
    );
  }
}